package com.yyfax.pay.common.exception;

import com.yyfax.framework.exception.YYException;
import com.yyfax.framework.yyfax.enums.RspCodeEnum;
import com.yyfax.pay.common.enums.PayRspCodeEnum;

/**
 * 支付订单异常<br>
 * 针对一些含有业务场景异常处理
 * 
 * @author buyi
 * @date 2017-05-06 13:38:28
 * @since v1.0.0
 */
@SuppressWarnings("serial")
public class YYPayOrderException extends YYPayException {

	/** 支付模块订单号 */
	private String tradeOrderId;
	private String merchantCode;

	public YYPayOrderException(Throwable cause) {
		super(cause);
	}

	public YYPayOrderException(String tradeOrderId, String merchantCode, Exception e) {
		if (e instanceof YYException) {
			YYException yyEx = (YYException) e;
			this.setCode(yyEx.getCode());
			this.setDisplay(yyEx.getDisplay());
		} else if (e instanceof YYPayException) {
			YYPayException yyEx = (YYPayException) e;
			this.setCode(yyEx.getCode());
			this.setDisplay(yyEx.getDisplay());
		} else {
			this.setCode(PayRspCodeEnum.COMMON_ERR0R_NET_ERROR.getValue());
			this.setDisplay(PayRspCodeEnum.COMMON_ERR0R_NET_ERROR.getDisplay());
		}

		this.setMessage(e.getMessage());
		this.setStackTrace(e.getStackTrace());

		this.setTradeOrderId(tradeOrderId);
		this.setMerchantCode(merchantCode);
	}

	public YYPayOrderException(RspCodeEnum code, String message, String tradeOrderId, String merchantCode) {
		super();
		this.code = Integer.valueOf(code.getValue());
		this.display = code.getDisplay();
		this.message = message;
		this.tradeOrderId = tradeOrderId;
		this.merchantCode = merchantCode;
	}

	public String getTradeOrderId() {
		return tradeOrderId;
	}

	public void setTradeOrderId(String tradeOrderId) {
		this.tradeOrderId = tradeOrderId;
	}

	public String getMerchantCode() {
		return merchantCode;
	}

	public void setMerchantCode(String merchantCode) {
		this.merchantCode = merchantCode;
	}

	/**
	 * 转换异常
	 * 
	 * @author buyi
	 * @date 2017-05-11 19:00:37
	 * @since v1.0.0
	 * @param tradeOrderId
	 * @param merchantCode
	 * @param e
	 */
	// public void convert(String tradeOrderId, String merchantCode, Exception e) {
	// if (e instanceof YYException || e instanceof YYPayException) {
	// YYException yyEx = (YYException) e;
	// this.setCode(yyEx.getCode());
	// this.setDisplay(yyEx.getDisplay());
	// } else {
	// this.setCode(PayRspCodeEnum.COMMON_ERR0R_NET_ERROR.getValue());
	// this.setDisplay(PayRspCodeEnum.COMMON_ERR0R_NET_ERROR.getDisplay());
	// }
	//
	// this.setMessage(e.getMessage());
	// this.setStackTrace(e.getStackTrace());
	//
	// this.setTradeOrderId(tradeOrderId);
	// this.setMerchantCode(merchantCode);
	// }
}
